package com.lin.missyou.util;

import com.lin.missyou.bo.PageCounter;

import java.math.BigDecimal;
import java.util.Calendar;
import java.util.Date;

/**
 * @author gcq
 * @Create 2021-10-15
 */
public class CommonUtil {

    public static PageCounter convertToPageParameter(Integer start, Integer count)
    {
        int pageNum = start / count;
        PageCounter pageCounter = PageCounter.builder()
                .count(count)
                .page(pageNum)
                .build();
        return pageCounter;
    }

    /**
     * 是否在指定时间内
     * @param date
     * @param start
     * @param end
     * @return
     */
    public static Boolean isInTimeLine(Date date, Date start, Date end)
    {
        Long time = date.getTime();
        Long startTime = start.getTime();
        Long endTime = end.getTime();
        if(time > startTime && time < endTime)
        {
            return true;
        }
        return false;
    }
    public static boolean isOutOfDate(Date startTime, Long period) {
        long now = Calendar.getInstance().getTimeInMillis();
        long startTimeStamp = startTime.getTime();
        long periodTimeStamp = period * 1000;
        return now > (startTimeStamp + periodTimeStamp);
    }

    /**
     * 比较订单时间
     * @param expiredTime
     * @return
     */
    public static boolean isOutOfDate(Date expiredTime) {
        long now = Calendar.getInstance().getTimeInMillis();
        return now > expiredTime.getTime();
    }

    public static Calendar addSomeSeconds(Calendar calendar,int second){
        calendar.add(Calendar.SECOND,second);
        return calendar;
    }
    /**
     * 元转换成分
     *
     * @param p 元
     * @return
     */
    public static String yuanToFenPlainString(BigDecimal p) {
        p = p.multiply(new BigDecimal("100"));
        return CommonUtil.toPlain(p);
    }
    /**
     * 将BigDecimal转换成字符串
     *
     * @param p
     * @return
     */
    public static String toPlain(BigDecimal p) {
        return p.stripTrailingZeros().toPlainString();
    }
    /**
     * 获取长度为10的时间戳
     *
     * @return 时间戳字符串
     */
    public static String timestamp10() {
        Long timestamp13 = Calendar.getInstance().getTimeInMillis();
        String timestamp13Str = timestamp13.toString();
        return timestamp13Str.substring(0, timestamp13Str.length() - 3);
    }
}